package ar.edu.caece.pl.asem.model.impl.treeelements;

import ar.edu.caece.pl.asem.model.impl.SymbolTable;
import ar.edu.caece.pl.asem.temp.TemporalGenerator;

public class Parametro extends SimboloGenerico {

	public static final int BYREF = 0;
	public static final int BYVAL = 1;

	private int mode; // Solo para Parametros.

	public Parametro(String name, int type, int mode) {
		super(name, type);
		this.mode = mode;
		this.label = "PARAMETRO";
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}

	@Override
	public String toString() {
		if (this.getMode() == BYREF) {
			return this.getLabel() + ": " + this.getName() + " ("
					+ SymbolTable.getVerboseType(this.getType()) + ") = BYREF";
		} else {
			return this.getLabel() + ": " + this.getName() + " ("
					+ this.getType() + ") = BYVAL";
		}
	}

	@Override
	public void generateASMCode() {
		StringBuffer sb = new StringBuffer();
		if (!(this.parent instanceof Declaraciones || this.parent instanceof Procedimiento || this.parent instanceof Parametros)) {
			TemporalGenerator.getInstance().generateTemporal();
			this.place = TemporalGenerator.getInstance().getActualTemporal();
			sb.append("\t mov ax, " + this.getEnvName() + "_" + this.getName()
					+ " \n");
			sb.append("\t mov " + this.place + ",ax \n");
		}
		
		if(this.parent instanceof Parametros){
			String key = this.getEnvName() + "_" + this.getName();
				sb.append(key + " dw "
						+ this.getValue() + " \n");
		}
		
		this.code = sb.toString();

	}

}
